package com.example.taobao.ui.home;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.taobao.GouwuMainActivity;
import com.example.taobao.GouwuMainActivity2;
import com.example.taobao.GouwuMainActivity3;
import com.example.taobao.GouwuMainActivity4;
import com.example.taobao.GouwuMainActivity5;
import com.example.taobao.GouwuMainActivity6;
import com.example.taobao.Huowu;
import com.example.taobao.R;
import com.example.taobao.databinding.FragmentHomeBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private FragmentHomeBinding binding;
    final int FLAG_MSG = 0x001;
    int[] flag= new int[]{R.drawable.imag111,R.drawable.img222,R.drawable.img333};
    private Message message;
    private Animation[] animation = new Animation[2];
    private ViewFlipper Flipper;





    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        int[] imagebutton = new int[]{R.drawable.image1,R.drawable.image2,
                R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6};//1



        String[] title = new String[]{"小风扇","YOUR DEsign运动水杯","女包","咸蛋黄端午节粽子","护肤套装","电动牙刷"};//1

        String[] jiage = new String[]{"19.9","199","799","369","99","469"};//1


        View view =inflater.inflate(R.layout.fragment_home,null);


        ListView listView =view.findViewById(R.id.homelistview);//1

        Flipper = view.findViewById(R.id.viewFlipper);




        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

//        final ListView listView = binding.homelistview;

        List<Map<String,Object>> listitem = new ArrayList<Map<String, Object>>();//1
        for (int i=0;i<imagebutton.length;i++){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("image",imagebutton[i]);
            map.put("title",title[i]);
            map.put("jiage",jiage[i]);
            listitem.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(getActivity(),listitem,
                R.layout.listlayout, new String[]{"image","title","jiage"},new int[]{R.id.imagebutton,R.id.nameWup,R.id.valueWup});

        listView.setAdapter(adapter);
        Toast.makeText(getActivity(), "ok" , Toast.LENGTH_SHORT).show();//1

//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                switch (i){
//                    case 0:
//                        Toast.makeText(getActivity(), "ok", Toast.LENGTH_SHORT).show();
//                        break;
//                    default:
//                        break;
//                }
//            }
//        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//1
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                String ValueWup;
                Double ValueWup;
                String WupName;
                Huowu p2 = new Huowu();
                Intent intent;
                Bundle bundle = new Bundle();
                switch (i){
                    case 0:
                        WupName = "小风扇";
                        ValueWup = 19.9;
                        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                        alert.setIcon(R.drawable.userimage);
                        alert.setMessage("是否加入购物车");
                        alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                p2.setMinchen(WupName);
                                p2.setJiage(ValueWup);
                                p2.setType("商品");
                                p2.setId("0");
//                        p2.setCount("1");
                                p2.setCount(1);
                                tianjia(p2);//1

                            }
                        });
                        alert.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alert.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                    Intent intent = new Intent(getActivity(), GouwuMainActivity.class);
                                    bundle.putString("name",WupName);
                                    bundle.putDouble("jiage",ValueWup);
                                    intent.putExtras(bundle);
                                    startActivityForResult(intent,1);
                            }
                        });
                        alert.create().show();
                        Toast.makeText(getActivity(), "okkk", Toast.LENGTH_SHORT).show();

                        break;
                    case 1:
                        WupName = "YOUR DEsign运动水杯";
//                        ValueWup = "199";
                        ValueWup = 199.0;
                        AlertDialog.Builder alert1 = new AlertDialog.Builder(getActivity());
                        alert1.setIcon(R.drawable.userimage);
                        alert1.setMessage("是否加入购物车");
                        alert1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                p2.setMinchen(WupName);
                                p2.setJiage(ValueWup);
                                p2.setType("商品");
                                p2.setId("1");
//                        p2.setCount("1");
                                p2.setCount(1);
                                tianjia(p2);//1

                            }
                        });
                        alert1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alert1.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent intent = new Intent(getActivity(), GouwuMainActivity2.class);
                                bundle.putString("name",WupName);
                                bundle.putDouble("jiage",ValueWup);
                                intent.putExtras(bundle);
                                startActivityForResult(intent,108);
                            }
                        });
                        alert1.create().show();
                        break;
                    case 2:
                        WupName = "女包";
//                        ValueWup = "799";
                        ValueWup = 799.0;
                        AlertDialog.Builder alert2 = new AlertDialog.Builder(getActivity());
                        alert2.setIcon(R.drawable.userimage);
                        alert2.setMessage("是否加入购物车");
                        alert2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                p2.setMinchen(WupName);
                                p2.setJiage(ValueWup);
                                p2.setType("商品");
                                p2.setId("2");
//                        p2.setCount("1");
                                p2.setCount(1);
                                tianjia(p2);//1

                            }
                        });
                        alert2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alert2.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent intent = new Intent(getActivity(), GouwuMainActivity3.class);
                                bundle.putString("name",WupName);
                                bundle.putDouble("jiage",ValueWup);
                                intent.putExtras(bundle);
                                startActivityForResult(intent,100);
                            }
                        });
                        alert2.create().show();
                        break;
                    case 3:
                        WupName = "咸蛋黄端午节粽子";
//                        ValueWup = "369";
                        ValueWup = 369.0;
                        AlertDialog.Builder alert3 = new AlertDialog.Builder(getActivity());
                        alert3.setIcon(R.drawable.userimage);
                        alert3.setMessage("是否加入购物车");
                        alert3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                p2.setMinchen(WupName);
                                p2.setJiage(ValueWup);
                                p2.setType("商品");
                                p2.setId("2");
//                        p2.setCount("1");
                                p2.setCount(1);
                                tianjia(p2);//1

                            }
                        });
                        alert3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alert3.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent intent = new Intent(getActivity(), GouwuMainActivity4.class);
                                bundle.putString("name",WupName);
                                bundle.putDouble("jiage",ValueWup);
                                intent.putExtras(bundle);
                                startActivityForResult(intent,101);
                            }
                        });
                        alert3.create().show();
                        break;
                    case 4:
                        WupName = "护肤套装";
//                        ValueWup = "99";
                        ValueWup = 99.0;
                        AlertDialog.Builder alert4 = new AlertDialog.Builder(getActivity());
                        alert4.setIcon(R.drawable.userimage);
                        alert4.setMessage("是否加入购物车");
                        alert4.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                p2.setMinchen(WupName);
                                p2.setJiage(ValueWup);
                                p2.setType("商品");
                                p2.setId("3");
//                        p2.setCount("1");
                                p2.setCount(1);
                                tianjia(p2);//1

                            }
                        });
                        alert4.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alert4.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent intent = new Intent(getActivity(), GouwuMainActivity5.class);
                                bundle.putString("name",WupName);
                                bundle.putDouble("jiage",ValueWup);
                                intent.putExtras(bundle);
                                startActivityForResult(intent,109);
                            }
                        });
                        alert4.create().show();
                        break;
                    case 5:
                        WupName = "电动牙刷";
//                        ValueWup = "469";
                        ValueWup = 469.0;
                        AlertDialog.Builder alert5 = new AlertDialog.Builder(getActivity());
                        alert5.setIcon(R.drawable.userimage);
                        alert5.setMessage("是否加入购物车");
                        alert5.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                p2.setMinchen(WupName);
                                p2.setJiage(ValueWup);
                                p2.setType("商品");
                                p2.setId("0");
//                        p2.setCount("1");
                                p2.setCount(1);
                                tianjia(p2);//1

                            }
                        });
                        alert5.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alert5.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent intent = new Intent(getActivity(), GouwuMainActivity6.class);
                                bundle.putString("name",WupName);
                                bundle.putDouble("jiage",ValueWup);
                                intent.putExtras(bundle);
                                startActivityForResult(intent,1);
                            }
                        });
                        alert5.create().show();
                        break;
                    default:
                        break;
                }
            }
        });


        for (int i = 0; i < flag.length; i++) {

            ImageView imageView = new ImageView(getActivity());
            imageView.setImageResource(flag[i]);
            Flipper.addView(imageView);
        }

        animation[0] = AnimationUtils.loadAnimation(getActivity(), R.anim.left_slide);
        animation[1] = AnimationUtils.loadAnimation(getActivity(), R.anim.right_slide);
        Flipper.setInAnimation(animation[1]);   //为flipper设置图片进入动画效果
        Flipper.setOutAnimation(animation[0]);  //为flipper设置图片退出动画效果

        message = Message.obtain();       //获得消息对象
        message.what = FLAG_MSG;  //设置消息代码
        handler.sendMessage(message);


        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
//                textView.setText(s);
            }
        });
        return view;
    }
    Handler handler = new Handler() {  //创建android.os.Handler对象
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == FLAG_MSG) {  //如果接收到的是发送的标记消息
                Flipper.showNext();                  //示下一张图片show
            }
            message = handler.obtainMessage(FLAG_MSG);   //获取要发送的消息
            handler.sendMessageDelayed(message, 3000);  //延迟3秒发送消息
        }
    };


    public void tianjia(Huowu p2){
        p2.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if(e==null){
                    Toast.makeText(getActivity(), "ok", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getActivity(), "fault", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}